/*
 * Sprite.h
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#pragma once

#include <SDL.h>

class Sprite
{
public:
    Sprite(SDL_Surface* image = NULL) :
        _image(image)
    {
        if (_image)
        {
            _rect.h = _image->h;
            _rect.w = _image->w;
        }
    }

    virtual ~Sprite() {};

    virtual bool update(void* arg) = 0;

    // damage object and receive responce damage
    virtual int damage(int amount) { return 0; };

    virtual void erase(SDL_Surface* screen, SDL_Surface* background)
    {
        SDL_BlitSurface(background, &_rect, screen, &_rect);
    }

    virtual void blit(SDL_Surface* screen)
    {
        SDL_BlitSurface(_image, NULL, screen, &_rect);
    }

    bool collides(const SDL_Rect& rect)
    {
        int _right = _rect.x + _rect.w;
        int right = rect.x + rect.w;
        int _bottom = _rect.y + _rect.h;
        int bottom = rect.y + rect.h;

        if (_bottom < rect.y) return false;
        if (_rect.y > bottom) return false;
        if (_right < rect.x) return false;
        if (_rect.x > right) return false;

        return true;
    }

    void move_to(int x, int y)
    {
        _rect.x = x - _rect.w / 2;
        _rect.y = y - _rect.h / 2;
    }

    void move(int x, int y)
    {
        _rect.x += x;
        _rect.y += y;
    }

    const SDL_Rect& rect() const { return _rect;  }
    SDL_Surface*    image()      { return _image; }
    int x() const { return _rect.x + _rect.w / 2; }
    int y() const { return _rect.y + _rect.h / 2; }

protected:
    SDL_Rect     _rect;
    SDL_Surface* _image;
};
